package com.kinghunt.misp.service.impl.userx;

import com.kinghunt.misp.common.dto.userx.RoleDTO;
import com.kinghunt.misp.common.param.userx.RoleQueryCondition;
import com.kinghunt.misp.common.param.userx.RoleQueryParam;
import com.kinghunt.misp.dal.dao.userx.RoleDAO;
import com.kinghunt.misp.dal.model.userx.RoleDO;
import com.kinghunt.misp.service.api.userx.RoleReadService;
import com.bygoodtech.kings.base.utils.ConvertUtil;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description 角色
 * @Author jiangtao
 * @Date 2023/7/5 22:16
 */
@Service
public class RoleReadServiceImpl implements RoleReadService {

    @Autowired(required = false)
    RoleDAO roleDAO;

    @Override
    public ApiResult<RoleDTO> getRoleById(Long id) {
        RoleDO roleDO = roleDAO.selectByPrimaryKey(id);
        RoleDTO dto = ConvertUtil.convert(roleDO, RoleDTO.class);
        return ApiResult.ok(dto);
    }

    @Override
    public ApiResult<PageInfo<RoleDTO>> pageRoleInfos(RoleQueryCondition condition) {
        PageInfo<RoleDTO> paging = new PageInfo<>();
        RoleQueryParam param = ConvertUtil.convert(condition, RoleQueryParam.class);
        param.setOffset((condition.getPageNum() - 1) * condition.getPageSize());
        Long total = roleDAO.getTotalCount(param);
        List<RoleDO> roleDOS = roleDAO.paging(param);
        List<RoleDTO> roleDTOs = ConvertUtil.convert(roleDOS, RoleDTO.class);

        paging.setTotal(total);
        paging.setData(roleDTOs);

        return ApiResult.ok(paging);
    }
}
